<template>
  <div>
    <div class="search-context">
      <div style="margin-top: 20px">
        <el-button type="primary" icon="el-icon-plus" @click="addDept">新增</el-button>
      </div>
    </div>
    <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">

      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="上级部门">
          <el-cascader :options="deptTreeRoot" :show-all-levels="false" v-model="form.parentId" :props="optionProps"
                       @change="selectParentDept">
          </el-cascader>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="form.deptName"></el-input>
        </el-form-item>
        <el-form-item label="序号">
          <el-input-number v-model="form.sort" :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="菜单状态">
          <el-switch
              v-model="form.deptStatus"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="form.chargePerson"></el-input>
        </el-form-item>
        <el-form-item label="负责人电话">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="负责人邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlerAddOrModifyDept">确 定</el-button>
      </span>
    </el-dialog>
    <div style="margin-top: 20px">
      <el-table
          :data="tableData"
          style="width: 100%"
          row-key="deptId"
          highlight-current-row
          :default-expand-all="true"
          :indent="20"
          @current-change="handleCurrentChange"
          border
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
            prop="parentId"
            v-if="false"
            label="上级部门Id"
            width="180">
        </el-table-column>
        <el-table-column
            prop="deptName"
            label="部门名"
            width="180">
        </el-table-column>
        <el-table-column
            prop="sort"
            label="排序"
            width="180">
        </el-table-column>
        <el-table-column
            prop="deptStatus"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.deptStatus"
                active-color="#13ce66"
                inactive-color="#ff4949" disabled>
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            prop="chargePerson"
            label="负责人">
        </el-table-column>
        <el-table-column
            prop="mobile"
            label="负责人电话">
        </el-table-column>
        <el-table-column
            prop="email"
            label="负责人邮箱">
        </el-table-column>
        <el-table-column
            prop="createDateTime"
            label="创建时间">
        </el-table-column>
        <el-table-column
            label="操作"
            width="180"
            align="center"
        >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="editDept(scope.row)"
                       size="mini"></el-button>
            <el-popover width="160" :ref="`popover-${scope.$index}`" placement="top">
              <p>确定删除该项吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button type="text" size="mini" @click="deleteHandleClose(scope.$index)">
                  取消
                </el-button>
                <el-button type="danger" size="mini" @click="removeDept(scope.row,scope.$index)">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" icon="el-icon-delete" circle size="mini"
                         style="margin-left: 10px"></el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: "dept",
  data() {
    return {
      optionProps: {
        value: 'deptId',
        label: 'deptName',
        emitPath: false,
        checkStrictly: true
      },
      form: {
        deptId: null,
        parentId: null,
        deptName: null,
        sort: null,
        deptStatus: true,
        chargePerson: null,
        mobile: null,
        email: null
      },
      dialogVisible: false,
      dialogTitle: '新增部门',
      deptTreeRoot: [{
        deptId: '0',
        parentId: '0',
        deptName: '根栏目',
        children: []
      }],
      tableData: [],
      currentRow: null
    }
  },
  created() {
    this.handlerTable();
  },
  methods: {
    handlerTable() {
      var param = {};
      this.systemRequest.deptTree(this, param, (res) => {
        console.log(res)
        if (res.data) {
          this.tableData = res.data;
          this.deptTreeRoot[0].children = [];

          if (this.tableData) {
            for (let i = 0; i < this.tableData.length; i++) {
              this.deptTreeRoot[0].children.push(this.tableData[i])
            }
          }
          console.log(this.deptTreeRoot)
        }
      })
    },
    handleCurrentChange(val) {
      this.currentRow = val;
      console.log(val)
    },
    handlerQuery() {
      console.log(this.searchDate)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(() => {
            done();
          }).catch(() => {
      });
    },
    addDept() {
      this.dialogTitle = '新增部门';
      this.dialogVisible = true;
      if (this.currentRow) {
        this.form.parentId = this.currentRow.deptId;
      } else {
        this.form.parentId = '0';
      }
      this.form.deptId = null;
      this.form.deptName = null;
      this.form.sort = null;
      this.form.deptStatus = true;
      this.form.chargePerson = null;
      this.form.mobile = null;
      this.form.email = null;
    },
    editDept(val) {
      this.dialogTitle = '编辑部门';
      this.dialogVisible = true;
      this.form.parentId = val.parentId;
      this.form.deptId = val.deptId;
      this.form.deptName = val.deptName;
      this.form.sort = val.sort;
      this.form.deptStatus = val.deptStatus;
      this.form.chargePerson = val.chargePerson;
      this.form.mobile = val.mobile;
      this.form.email = val.email;
      console.log(val.deptName);
    },
    /**
     * 添加或修改部门
     */
    handlerAddOrModifyDept() {
      var param = this.form;
      if (this.form.deptId) {
        this.systemRequest.modifyDept(this, param, (res) => {
          console.log(res)
          this.dialogVisible = false;
          this.$message.success("部门信息已修改");
          this.handlerTable();
        })
      } else {
        this.systemRequest.addDept(this, param, (res) => {
          console.log(res)
          this.dialogVisible = false;
          this.$message.success("部门信息已添加");
          this.handlerTable();
        })
      }
    },
    deleteHandleClose(index) {
      this.$refs[`popover-${index}`].doClose()
    },
    removeDept(val, index) {
      console.log(val)
      var param = {
        deptId: val.deptId
      };
      this.systemRequest.deleteDept(this, param, (res) => {
        console.log(res)
        this.$message.success("成功删除部门");
        this.handlerTable();
      })

      this.deleteHandleClose(index);
    },
    selectParentDept(val) {
      console.log(val)
    }
  },
}
</script>

<style scoped>

</style>